In [ ]:
Most Python functions and objects can provide documentation via help function. Look the documentation of e.g open function with help(open)
In [ ]:
Play with tabulator completion, by typing just pr and pressing then tabulator key. Pressing Shift-tab (after finalising completion) one sees also short documentation about the function or object. This works also on variable names, try e.g.
my_extremely_long_variable_name = 5
my <TAB>
In [ ]:
In [ ]:
You probably noticed that even though print is a method in the namespace it was still valid to create a variable called print. If you now try to actually print something, you will get an error. For built-in functions (such as print) one can recover with the following code
In [ ]:
print = __builtin__.print
print("hello")
Are the following pieces valid Python code?
Case 1
numbers = [4, 5, 6, 9, 11]
sum = 0
for n in numbers:
sum += n
print("Sum is now"), sum
Case 2
x = 11
test(x)
def test(a):
if a < 0:
print("negative number")
In [ ]:
In [ ]:
Attempt to append the string "spam"
to mylist and mytuple using append.
In [ ]:
List objects have a sort() function, use that for sorting the list alphabetically (e.g. mylist.sort() ). What is now the first item of the list?
Next, remove the first item from the list, investigate the contents and remove then last item from the list.
In [ ]:
In [ ]:
Using slicing syntax, select
In [ ]:
Read up on the stride syntax . Then using it select
In [ ]:
In [ ]:
Create a dictionary whose keys are the fruits “pineapple”, “strawberry”, and “banana”. As values use numbers representing e.g. prices.
Add “orange” to the dictionary and then remove “banana” from the dictionary. Investigate the contents of dictionary and pay attention to the order of key-value pairs.
In [ ]:
In [ ]:
It is often useful idiom to create empty lists or dictionaries and add contents little by little.
Create first an empty dictionary for a mid-term grades of students. Then, add a key-value pairs where the keys are student names and the values are empty lists.
Finally, add values to the lists and investigate the contents of the dictionary.
In [ ]: